﻿using System;
using System.Web.Security;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Eagle.Common
{
    /// <summary>
    /// 加密解密类
    /// </summary>
    public class EagleSecurity
    {
        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value">要加密的字符串</param>
        /// <returns></returns>
        public static string Md5(string value)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(value, "MD5");
        }

        #endregion

        #region SHA1加密
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="value">要加密的字符串</param>
        /// <returns></returns>
        public static string Sha1(string value)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(value, "SHA1");
        }
        #endregion

        #region AES加密解密

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="value">要加密的字符串</param>
        /// <param name="key">加密密钥</param>
        /// <returns></returns>
        public static string Aes(string value, string key)
        {
            byte[] keyvector = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x4a, 0x61, 0x6e, 0x73, 0x2e };
            RijndaelManaged rm = new RijndaelManaged();
            rm.IV = keyvector;
            if (key.Length > 32)
                key = key.Substring(0, 32);
            if (key.Length == 0)
                key = "you forget write key,we help you";
            rm.Key = Encoding.UTF8.GetBytes(key.PadRight(32, ' '));
            using (ICryptoTransform ICT = rm.CreateEncryptor())
            {
                byte[] inputData = Encoding.UTF8.GetBytes(value);
                byte[] encryptedData = ICT.TransformFinalBlock(inputData, 0, inputData.Length);
                rm.Clear();
                return Convert.ToBase64String(encryptedData);
            }
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="value">要解密的字符串</param>
        /// <param name="key">解密密钥</param>
        /// <returns></returns>
        public static string DecodeAes(string value, string key)
        {
            byte[] keyvector = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x49, 0x20, 0x61, 0x6d, 0x20, 0x4a, 0x61, 0x6e, 0x73, 0x2e };
            RijndaelManaged rm = new RijndaelManaged();
            rm.IV = keyvector;
            if (key.Length > 32)
                key = key.Substring(0, 32);
            if (key.Length == 0)
                key = "you forget write key,we help you";
            rm.Key = Encoding.UTF8.GetBytes(key.PadRight(32, ' '));
            using (ICryptoTransform ICT = rm.CreateDecryptor())
            {
                byte[] inputValue = Convert.FromBase64String(value);
                byte[] decryptedValue = ICT.TransformFinalBlock(inputValue, 0, inputValue.Length);
                rm.Clear();
                return Encoding.UTF8.GetString(decryptedValue);
            }
        }

        #endregion

    }
}
